<template>
  <div>
    <log-reg-header></log-reg-header>
    <el-main>
      <div id="tips">
        <p>请填写注册信息，点击“提交注册”即可完成注册！</p>
      </div>
      <el-form
        id="register_form"
        :model="registerForm"
        :rules="registerFormRules"
        ref="registerFormRef"
        label-width="100px"
        hide-required-asterisk
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="registerForm.username"></el-input>
        </el-form-item>
        <p class="regTips">用户名为4~16位字母，数字，符号或中文</p>
        <el-form-item label="密 码" prop="password">
          <el-input v-model="registerForm.password" type="password"></el-input>
        </el-form-item>
        <p class="regTips">密码为4~16位字符组成,采用数字、字母、符号安全性更高</p>
        <el-form-item label="确认密码" prop="confirmPwd">
          <el-input v-model="registerForm.confirmPwd" type="password"></el-input>
        </el-form-item>
        <p class="regTips">请再次填写密码</p>
        <el-form-item>
          <el-button type="success" @click="register">同意协议并注册</el-button>
          <a href="/login">已有账号？去登录</a>
          <p class="instructions"><a href="#">《使用协议说明书》</a></p>
        </el-form-item>
      </el-form>
    </el-main>
    <el-dialog title="提示" :visible.sync="registerDialogVisible" width="50%">
      <span>注册成功，点击确定进入登录页面</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="registerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="toLogin">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// 导入 头部组件
import logRegHeader from "./components/login_reg/Header";
export default {
  data() {
    // 确认密码校验规则
    var checkConfirmPwd = (rule, value, callback) => {
      // 三个参数：校验规则，待校验的值，回调函数
      //没输入或两次密码相当 直接返回回调函数
      if (!value || value === this.registerForm.password) return callback();
      // 两次密码不等则返回错误信息
      callback(new Error("两次密码不一致，请重新输入"));
    };
    return {
      // 注册表单绑定的数据
      registerForm: {
        username: "王花花花",
        password: "1111",
        confirmPwd: "1111"
      },
      //注册表单校验规则
      registerFormRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 4,
            max: 16,
            pattern: /^[\u4e00-\u9fa50-9A-Za-z_]+$/,
            message: "用户名为4~16位字母，数字或下划线",
            trigger: "blur"
          },
          {
            pattern: /^[\u4e00-\u9fa50-9A-Za-z_]{4,16}$/,
            message: "用户名为4~16位字母，数字或下划线",
            trigger: "blur"
          }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 4, max: 16, message: "长度在4-6之间", trigger: "change" }
        ],
        confirmPwd: [
          { required: true, message: "请再次输入密码", trigger: "blur" },
          { validator: checkConfirmPwd, trigger: "blur" }
        ]
      },
      //控制注册成功弹出框显示隐藏
      registerDialogVisible:false,
    };
  },
  methods: {
    //注册
    register() {
      //表单预校验
      this.$refs.registerFormRef.validate(async valid=>{
        if(!valid) return 
        const res=await this.$http.post('/register.do',this.$qs.stringify(this.registerForm))
        // console.log(res)
        if(res.status!==200) return this.$message.error('注册失败')
        if(res.data.success!==true) return this.$message.error(res.data.msg)
        this.registerDialogVisible=true
      })
    },
    // 注册弹出框确定事件
    toLogin(){
      this.registerDialogVisible=false;
      this.$router.push('/login')
    }
  },
  components: {
    logRegHeader
  }
};
</script>

<style lang="less" scoped>
.el-main {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.el-form {
  width: 570px;
}
#tips {
  p {
    color: #888;
    font-size: 19px;
  }
}
.el-button {
  margin-right: 25px;
}
.el-input {
  display: inline-block;
  width: 230px;
}
.el-form-item {
  display: inline-block;
  margin-bottom: 0;
}
.regTips {
  color: #737373;
  margin: 0 100px;
  line-height: 25px;
  font-size: 14px;
  margin-bottom: 22px;
}
.instructions{
  margin: 0;
}
</style>